import {
  SEARCH_BLUR,
  SEARCH_FOCUS,
  CHANGE_SEARCH_LIST,
  MOUSE_LEAVE,
  MOUSE_ENTER,
  CHANGE_PAGE_LIST
} from './actionType'
import { fromJS } from 'immutable'

const defaultState = fromJS({
  focused: false,
  mouseIn: false,
  list: [],
  pageSize: 10,
  page: 1,
  totalPage: 1
})

export default (prevState = defaultState, action) => {
  switch (action.type) {
    case SEARCH_BLUR: {
      return prevState.set('focused', false)
    }

    case SEARCH_FOCUS: {
      return prevState.set('focused', true)
    }
    case CHANGE_SEARCH_LIST: {
      return prevState.merge({
        list: fromJS(action.data),
        totalPage: action.totalPage
      })
      // return prevState.set('list', fromJS(action.data)).set('totalPage', action.totalPage)
    }
    case MOUSE_LEAVE: {
      return prevState.set('mouseIn', false)
    }
    case MOUSE_ENTER: {
      return prevState.set('mouseIn', true)
    }
    case CHANGE_PAGE_LIST: {
      return prevState.set('page', action.page++ === action.totalPage ? 1 : action.page)
    }

    default:
      break
  }
  return prevState
}
